import { resetRouter } from '@/router'
// initial state
let state = {
  user: {},
  form: {
    cookie: false,
    username: "",
    password: "",
  },
  token: '',
  roles: false, // 路由是否处理判断 
}

const user = window.localStorage.getItem('user')
if (user) {
  state = JSON.parse(user)
}

/**
 * Getters
 * @type {import('vuex/types').GetterTree<typeof state>}
 */
const getters = {
  // getState: state => {
  //   return state
  // },
  // getUser: state => {
  //   return state.user
  // },
  // getForm: state => {
  //   return state.form
  // },
  // getToken: state => {
  //   return state.token
  // },
}
/**
 * Mutations
 *  @type {import('vuex/types').MutationTree<typeof state>}
 */
const mutations = {
  setUser(state, user) {
    Object.assign(state.user, user)
  },
  setForm(state, form) {
    Object.assign(state.form, form)
  },
  setToken(state, obj) {
    state.token = obj
  },
  setRoles: (state, roles) => {
    state.roles = roles
  }
}
/**
 * Actions
 *  @type {import('vuex/types').ActionTree<typeof state>}
 */
const actions = {
  logOut({
    commit
  }, obj) {
    commit('setUser')
    commit('setToken')
    commit('setRoles', false)
    resetRouter()
  },
}

export default {
  namespaced: true, // 为模块开启命名空间
  state,
  getters,
  mutations,
  actions
}
